package com.ymatou.iapi.sellerproduct.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.ymt.core.tool.Logger;
import com.ymttest.business.YmatouAutoTestCall;
import com.ymttest.business.service.AssertService;
import com.ymttest.database.mongo.ProductBasicInfoWapper;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductListingModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

public class BatchSetProductOnSaleCall extends YmatouAutoTestCall{
	private static String opurl ="/api/Product/BatchSetProductOnSale";
	
	public BatchSetProductOnSaleCall() {
		super(EnvSetup.getSystemUrl("sellerproduct_iapi"),opurl,"POST","JSON");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Accept","text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 接口返回信息公用信息部分断言（正向Case场景）
	 * @param dataStr 传入报文信息
	 * @throws Exception 错误信息
	 */
	public void getreturncodecheck(JSONObject dataStr,String productID1) throws Exception {
	    try {
			//返回报文断言
			String code =  dataStr.getString("Code");
			Logger.verifyIsContains(200, code, "\"Code\":200");
			String bCode =  dataStr.getString("BCode");
			Logger.verifyIsContains(0, bCode, "\"BCode\":0");
			String msg =  dataStr.getString("Msg");
			Logger.verifyIsContains("", msg, "Msg操作成功");
			String trace =  dataStr.getString("Trace");
			Logger.verifyIsNull(trace, "Trace等于空");			

	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	
	/**
	 * 批量上架断言
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param productidList 商品列表
	 * @throws Exception
	 */
	public void setOnSalecheck(String startTime , String endTime , List<String> productidList) throws Exception {
	    try {

			for (String productid : productidList) {
				//SQL YmtProducts
				List<Map> productIDsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productid);
				String svalidStartbysql = (new SimpleDateFormat("yyyy-MM-dd")).format(productIDsqllist.get(0).get("validStart"));			
				String svalidEndbysql = (new SimpleDateFormat("yyyy-MM-dd")).format(productIDsqllist.get(0).get("validEnd"));						
				Logger.verifyEquals(startTime, svalidStartbysql, "商品开始销售时间");
				Logger.verifyEquals(endTime, svalidEndbysql, "商品结束销售时间");
				
				if (YmtMongoDriverFactroy.isOldMongoCheck()){				
					//Mango ProductBasicInfo
					Map<String, Object> productBasicInfo = new ProductBasicInfoWapper().getProductId(productid); 		
					String validStartbyMango = (new SimpleDateFormat("yyyy-MM-dd")).format(productBasicInfo.get("ValidStart"));	
					String ValidEndbyMango = (new SimpleDateFormat("yyyy-MM-dd")).format(productBasicInfo.get("ValidEnd"));	
					Logger.verifyEquals(startTime, validStartbyMango, "商品开始销售时间");
					Logger.verifyEquals(endTime, ValidEndbyMango, "商品结束销售时间");		
				}
				

				if (YmtMongoDriverFactroy.isNewMongoCheck()){
					//newMango ProductDetailModel
					Map<String,Object> productDetailModel = new ProductDetailModelWapper().getProductId(productid);
					AssertService.assertTime(YMTDateUtil.getDate(), (Date)productDetailModel.get("ValidStart"), 1000);
					AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productDetailModel.get("ValidEnd"), 1000);
													
					//newMango ProductListingModel
					Map<String,Object> productListingModel = new ProductListingModelWapper().getProductId(productid);
					AssertService.assertTime(YMTDateUtil.getDate(), (Date)productListingModel.get("ValidStart"), 1000);
					AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productListingModel.get("ValidEnd"), 1000);	
					
					
				}
				
			}

	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}

	
	
	/**
	 * 更新库存
	 * @param iNum 库存数量
	 * @param productID 商品id
	 * @return
	 * @throws Exception
	 */
	public String updatestocknum(int iNum ,String productID) throws Exception {
	    try {	    	
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			ymtCatalogsIWapper.updatestocknumByCatalogId(iNum , productID);
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	
	
}
